package com.sc.controller;

import com.github.pagehelper.PageInfo;
import com.sc.pojo.Role;
import com.sc.service.MenuService;
import com.sc.service.RoleSerice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Map;

@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    RoleSerice rs;
    @Autowired
    MenuService ms;
    @RequestMapping("/show")
    public String show(@RequestParam(defaultValue = "1")Integer pageNum,
                       @RequestParam(defaultValue = "4")Integer pageSize, Map m,String keyword) {
        PageInfo<Role> p = rs.show(pageNum,pageSize,keyword);
        m.put("p",p);
        return "/admin/role/list";
    }
    @RequestMapping("/allMenu")
    public String allMenu(Integer id,Map m) {
        m.put("r",rs.query(id));
        m.put("menus",ms.queryAll());
        return "/admin/role/resources";
    }
    @RequestMapping("/saveMenu")
    public String saveMenu(Integer[] menuIds,Integer roleid) {
        System.out.println("角色id"+roleid);
        if (menuIds != null) {
            rs.saveMenu(menuIds,roleid);
        }
        return "redirect:/role/show";
    }
    @RequestMapping("/add")
    public String add(Role r) {
        rs.add(r);
        return "redirect:/role/show";
    }
    @RequestMapping("/del")
    public String del(Integer id) {
        rs.del(id);
        return "redirect:/role/show";
    }
    @RequestMapping("/upd")
    public String upd(Role r) {
        rs.upd(r);
        return "redirect:/role/show";
    }
    @RequestMapping("/delAll")
    public String delAll(Integer[] id) {
        if (id != null)rs.delAll(id);
        return "redirect:/role/show";
    }
    @RequestMapping("/change")
    public String change(Integer[] id,String type) {
        if (id != null) {
            if ("open".equals(type)) {
                rs.openAll(id);
            } else if ("close".equals(type)) {
                rs.closeAll(id);
            }
        }
        return "redirect:/role/show";
    }

}
